package com.sgb.packers.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.net.entity.ListResponse;
import com.sgb.packers.api.PackersNet;
import com.sgb.packers.entity.ProjectEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

/**
 * @author: 王彬臣
 * @date: 2021/11/25
 * @Description:
 */
public class ProjectListViewModel extends BaseViewModel {
    public MutableLiveData<Boolean> mAddProjectLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> mDelProject=new MutableLiveData<>();
    public MutableLiveData<Boolean> mCodeResult=new MutableLiveData<>();
    public MutableLiveData<ListResponse<List<ProjectEntity>>> mGetProjectLiveData = new MutableLiveData<>();
    public ProjectListViewModel(@NonNull @NotNull Application application) {
        super(application);
    }
    /**
     * 新增或编辑项目
     * @param map
     */
    public void addProject(Map<String,Object> map){
        mLoading.postValue(SHOW_LODING);
        PackersNet.getInstance().getService().addProject(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                mAddProjectLiveData.postValue(true);
            }
        });
    }

    /**
     * 获取项目列表
     * @param map
     */
    public int pageNum = 1;
    public void getProject(boolean isLoadMore,Map<String,Object> map){
        mLoading.postValue(SHOW_LODING);
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("page", pageNum);
        map.put("limit", 20);
        PackersNet.getInstance().getService().getProjectList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<ProjectEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<ProjectEntity>>> data) {
                mGetProjectLiveData.postValue(data.getData());
            }
        });
    }

    /**
     * 删除项目
     * @param no  项目no
     * @param code 短信验证码
     */
    public void delProject(String no,String code){
        mLoading.postValue(SHOW_LODING);
        String phone= SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, "");
        PackersNet.getInstance().getService().delProject(no,phone,code).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                mDelProject.postValue(true);
            }
        });
    }

    /**
     * 发短信
     */
    public void sendCode(){
        mLoading.postValue(SHOW_LODING);
        String phone= SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, "");
        PackersNet.getInstance().getService().sendCode(phone).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                mCodeResult.postValue(true);
            }
        });
    }
}
